package me.heizi.springboot.students

import me.heizi.springboot.students.config.StudentManageApplication
import me.heizi.springboot.students.controllers.apis.LoginRest
import me.heizi.springboot.students.controllers.apis.UserRest
import me.heizi.springboot.students.controllers.dto.UserDTO
import me.heizi.springboot.students.entities.User
import me.heizi.springboot.students.model.UserType
import me.heizi.springboot.students.repositories.UserRepository
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import javax.sql.DataSource

@SpringBootTest(classes = [StudentManageApplication::class])
class StudentManageApplicationTests {

    @Test fun `加载SpringBoot上下文`() {}

    @Test fun `select 语句在线证明SQL配置正确`(@Autowired dataSource: DataSource) {  //过 配置在线
        dataSource.connection.
        prepareStatement("select name from user_types where id = (select max(id) from user_types)  ").
        executeQuery().let { set -> if (set.next()) assert(set.getString("name") == "学生") }
    }

    @Test fun `测试MyBatis Mapper是否可用`(@Autowired users: UserRepository) = kotlin.runCatching { //配置正确 过
            users.selectList(null).forEach(::println)
    }.onFailure {
        println("error${it.message}")
    }.isSuccess.let(::assert)

}
@SpringBootTest(classes = [StudentManageApplication::class])
class `API合理测试` {
    @Test fun `更新用户名`(@Autowired ctrl: UserRest) { //合理了
        //更新用户名
        ctrl.updateByName(UserDTO(password = "abcd"),username = "vvv")
        assert(ctrl.all.find { it.id == 2 }?.password == "abcd")
    }
    @Test fun `登入`(@Autowired login: LoginRest) {

    }
}
@SpringBootTest(classes = [StudentManageApplication::class])
class `数据库相关测试` {


    @Test fun `TypeDao正确`(@Autowired typeKvs:List<UserType>) {
        println(typeKvs)
        assert(typeKvs.size==3)
    }
    @Test fun `通过用户名获取用户信息`(@Autowired users: UserRepository) {
        (users.selectByName("vvv")?.realName=="李送")
    }

    @Test fun `查找已存在的用户`(@Autowired users: UserRepository) {
        val byUserName = users.exist("vvv","immasend")
        val byPhone = users.exist("12312341236","immasend")
        assert(byPhone.size==1)
        assert(byUserName.size==1)
        assert(byPhone[0] == byUserName[0])

    }

    @Test fun `错误密码不可查找`(@Autowired users: UserRepository) {
        val byUserName = users.exist("vvv","wrong password")
        val byPhone = users.exist("12312341236","wrong password")
        assert(byPhone.isEmpty())
        assert(byUserName.isEmpty())
    }

    @Test fun `sql注入`(@Autowired users: UserRepository) {
        users.exist("vvv","wrong password' and 1=1 limit 1 --").let {
            assert(it.isEmpty())
        }
    }

}



//@SpringBootTest(classes = [StudentManageApplication::class])
//class `事物管理`() {
//
//    @Transactional
//    fun nothings() {
//        throw Exception()
//    }
//
//    fun `一定报错`() {
//
//    }
//}

//class JustLab {
//    @Test fun `反射赋值`() {
//        val a = UserRest.UserDTO(3)
//        a::class.memberProperties
//            .forEach {
//                println(it)
//                println(it.name)
//                runCatching {(it as KMutableProperty1<UserRest.UserDTO,Any> ).set(a,"anc")
//                }
//            }
//        println(a)
//    }
//}
